/* ===== playingmethod =====：玩法 */
myApp.onPageInit('playingmethodlist', function (page) {
	console.log("playingmethodlist");
    console.log(global_cityid);
    var playingmethod_city = global_cityid;
  
    //下拉加载的参数初始化
    // Loading flag
	var loading = false;
    // Append items per load
	var playingmenthoditemsPerLoad = playingmenthodpageitem;
	
	//禁止微信浏览器的下拉弹性效果
	
		
    //city
    $$.getJSON(url_get_city, {}, function (ajaxresult) {
        //获取city数据
        if(ajaxresult.code==1){
            var data_playingmethodcity = ajaxresult.data;
            var html_playingmethodcity = myApp.playingMethodCityTemplate(data_playingmethodcity);
            html_playingmethodcity="<li><div class='row' style='margin:5px;justify-content: flex-start;'><div class='col-33' style='width: calc(100%/3);margin-bottom: 0.5vh;margin-top: 0.5vh;'>" +
            		"<a href='#' class='button btn_item size_12' style='width:90%;margin:0 auto;' " +
            		"data-city='不限' data-id='0'>不限</a></div></li>"+html_playingmethodcity;
            $$('div.playingmethodcity ul').html(html_playingmethodcity);
        }else{
            ajaxfail(ajaxresult.msg);
        }
        //选定全局城市
        var activedom = ".playingmethodcity a.btn_item[data-id='" + playingmethod_city.toString() + "']";
        $$(activedom).addClass('active');
        //更新标题
        var city = '';
        city=$$(activedom).html(); 
        $$('.playingmethodcity_title span').html(city);
        
        //click city
        $$('.playingmethodcity a.btn_item').click(function(){
            $$('.playingmethodcity a.btn_item').removeClass('active');
            $$(this).addClass('active');
            //更新标题
            if($$(this).data('id')=='0'){
            	city='城市';
            }else{city=$$(this).html();} 
            $$('.playingmethodcity_title span').html(city);
            //刷新页面
            getplaymethodlist(0,playingmenthodpageitem,'refresh');
        });

    }); 
    //type
	$$.getJSON(url_get_playingmethodtype, {}, function (ajaxresult) {
		console.log(ajaxresult);
		if(ajaxresult.code==1){					
			var data_playingmethodtype = ajaxresult.data;
			var html_playingmethodtype = myApp.playingmethodtypeTemplate(data_playingmethodtype);
    		//html_playingmethodtype+="<div class='col-33'></div>";
            $$('div.playingmethodtype ul').html(html_playingmethodtype);
            //type click
            $$('.playingmethodtype a.btn_item').click(function(){
                $$('.playingmethodtype a.btn_item').removeClass('active');
                $$(this).addClass('active');
                //更新标题
                var type = '';
                if($$(this).data('id')=='0'){
                	type='类型';
                }else{type=$$(this).html();} 
                $$('.playingmethodtype_title span').html(type);
                //刷新页面
                getplaymethodlist(0,playingmenthodpageitem,'refresh');
            });
		}else{
			ajaxfail(ajaxresult.msg);
		}
	});
    // init list
	$$.getJSON(url_get_playingmethodlist, {city:global_cityid,offset:0,limit:playingmenthodpageitem}, function (ajaxresult) {
		console.log(ajaxresult);
		if(ajaxresult.code==1){
			var data_playingmethodcontent = ajaxresult.data;
			var html_playingmethodcontent = myApp.playItemTemplate(data_playingmethodcontent);
    		$$('#playingmethodlist .div_playingmethodcontent').html(html_playingmethodcontent);
    		
            //页面第一次加载
    		// Last loaded index
    		var lastIndex=$$('.div_playingmethodcontent .playitem').length;
    		$$("input[name='playingmethod_lastIndex']").val(lastIndex); 
    		
		}else{
			ajaxfail(ajaxresult.msg);
		}
	});
	//绑定滚动加载事件
	$$('#playingmethodlist .infinite-scroll').on('infinite', function () {
		  // Exit, if loading in progress
		  if (loading) return;		 
		  // Set loading flag
		  loading = true;
		  // Emulate 1s loading
		  setTimeout(function () {
		  // Reset loading flag
		  loading = false;
		  // last load index
		  lastIndex = $$("input[name='playingmethod_lastIndex']").val();
		  // Generate new items HTML and Update last loaded index
		  getplaymethodlist(lastIndex,playingmenthoditemsPerLoad,'append'); 		  
	 }, 1000);
	});
	//subnav的收放  
    $$(".playingmethodcity_title").click(function(){
    	var stop=false;
    	//关闭其他
    	if ($$('.playingmethodtype').hasClass('grade-w-roll')){
            $$('.playingmethodtype').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.playingmethodsort').hasClass('grade-w-roll')){
            $$('.playingmethodsort').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.playingmethodfilter').hasClass('grade-w-roll')){
            $$('.playingmethodfilter').removeClass('grade-w-roll');
            stop=true;
        };
        //打开或开启自身
        if ($$('.playingmethodcity').hasClass('grade-w-roll')) {
            $$('.playingmethodcity').removeClass('grade-w-roll');
            $$("#playingmethodlist").removeClass('with-shadow');
        }else {       	
        	if(stop){
        		setTimeout("$$('.playingmethodcity').addClass('grade-w-roll')",500);
        	}else{
        		$$("#playingmethodlist").addClass('with-shadow');
                $$('.playingmethodcity').addClass('grade-w-roll');
            }
        }        
    });
    
    $$(".playingmethodtype_title").click(function(){
    	var stop=false;
    	if ($$('.playingmethodcity').hasClass('grade-w-roll')){
            $$('.playingmethodcity').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.playingmethodsort').hasClass('grade-w-roll')){
            $$('.playingmethodsort').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.playingmethodfilter').hasClass('grade-w-roll')){
            $$('.playingmethodfilter').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.playingmethodtype').hasClass('grade-w-roll')) {
            $$('.playingmethodtype').removeClass('grade-w-roll');
            $$("#playingmethodlist").removeClass('with-shadow');
        }else {
        	if(stop){
        		$$("#playingmethodlist").addClass('with-shadow');
        		setTimeout("$$('.playingmethodtype').addClass('grade-w-roll')",500);
        	}else{
               $$('.playingmethodtype').addClass('grade-w-roll');
            }
        }        
    });
    
    $$(".playingmethodsort_title").click(function(){
    	var stop=false;
    	if ($$('.playingmethodtype').hasClass('grade-w-roll')){
            $$('.playingmethodtype').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.playingmethodcity').hasClass('grade-w-roll')){
            $$('.playingmethodcity').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.playingmethodfilter').hasClass('grade-w-roll')){
            $$('.playingmethodfilter').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.playingmethodsort').hasClass('grade-w-roll')) {
            $$('.playingmethodsort').removeClass('grade-w-roll');
            $$("#playingmethodlist").removeClass('with-shadow');
        }else {
        	if(stop){
        		$$("#playingmethodlist").addClass('with-shadow');
        		setTimeout("$$('.playingmethodsort').addClass('grade-w-roll')",500);
        	}else{
                $$('.playingmethodsort').addClass('grade-w-roll');
            }
        }        
    });
    
    $$(".playingmethodfilter_title").click(function(){
    	var stop=false;
    	if ($$('.playingmethodtype').hasClass('grade-w-roll')){
            $$('.playingmethodtype').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.playingmethodsort').hasClass('grade-w-roll')){
            $$('.playingmethodsort').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.playingmethodcity').hasClass('grade-w-roll')){
            $$('.playingmethodcity').removeClass('grade-w-roll');
            stop=true;
        };
        if ($$('.playingmethodfilter').hasClass('grade-w-roll')) {
            $$('.playingmethodfilter').removeClass('grade-w-roll');
            $$("#playingmethodlist").removeClass('with-shadow');
        }else {
        	if(stop){
        		setTimeout("$$('.playingmethodfilter').addClass('grade-w-roll')",500);
        	}else{
        		$$("#playingmethodlist").addClass('with-shadow');
                $$('.playingmethodfilter').addClass('grade-w-roll'); 
            }
        }        
    });
    //排序click   
    $$('.playingmethodsort a.btn_item').click(function(){
    	$$('.playingmethodsort a.btn_item').removeClass('active');
    	$$(this).addClass('active');
    	//更新标题
    	var sort = '';
        if($$(this).data('id')=='0'){
        	sort='排序';
        }else{sort=$$(this).html();}     
        $$('.playingmethodsort_title span').html(sort);
    	//刷新页面
    	getplaymethodlist(0,playingmenthodpageitem,'refresh');
    });
    //筛选click
    $$('.playingmethodfilter .gender a.btn_item').click(function(){
    	$$('.playingmethodfilter .gender a.btn_item').removeClass('active');
    	$$(this).addClass('active');
    });
    
    $$('.playingmethodfilter .age a.btn_item').click(function(){
    	$$('.playingmethodfilter .age a.btn_item').removeClass('active');
    	$$(this).addClass('active');
    });
    
    $$('.playingmethodfilter .traffic a.btn_item').click(function(){
    	$$('.playingmethodfilter .traffic a.btn_item').removeClass('active');
    	$$(this).addClass('active');
    });
    
    $$('.playingmethodfilter .role a.btn_item').click(function(){
    	$$('.playingmethodfilter .role a.btn_item').removeClass('active');
    	$$(this).addClass('active');
    });

    $$('#playingmethodsubnav .filter_submit').on('click',function(){
    	//刷新页面
    	getplaymethodlist(0,playingmenthodpageitem,'refresh');
    });
	
});

/* ===== playingmethoddetail =====：玩法详情页 */
myApp.onPageInit('playingmethoddetail', function (page) {
	console.log('playingmethoddetail');
    var id = $$("#playingmethoddetail .page-content").data("id");
    var type =  $$("#playingmethoddetail .page-content").data("type");
    console.log(id,type);
    var imageswall = [];
    //图片墙
    $$.ajax({
        url:url_get_imageswall,
        method:"GET",
        data:{
            id:id,
            type:type
        },
        dataType:"json",
        async:true,
        success:function(ajaxresult){
            if(ajaxresult.code==1){
                var _imagewalllist = ajaxresult.data;
                for(var i=0;i<_imagewalllist.length;i++){
                    var _temp = {
                        url: _imagewalllist[i].image,
                        caption: _imagewalllist[i].name
                    };
                    imageswall.push(_temp);
                }
               //picture ppt
                var myPhotoBrowserPopupDark = myApp.photoBrowser({
                    photos : imageswall,
                    theme: 'dark',
                    type: 'standalone',
                    backLinkText: '',
                    toolbar:false,
                    ofText:'/',
                    lazyLoading:true,
                });
                $$('#playingmethoddetail .imageswall').on('click', function () {        
                    myPhotoBrowserPopupDark.open();
                });
            }else{
                ajaxfail(ajaxresult.msg);
            }

        },
    });
    
    //集合地点地图
    if($$('#playingmethoddetail .page-content').data('type')=="ACTIVITY"){
    	var lat=$$('#playingmethoddetail .playmethodaddrimg').data('lat');
    	var lng=$$('#playingmethoddetail .playmethodaddrimg').data('lng');
    	//设置地图中心
    	if(lat&&lng){
    		var myLatlng=new google.maps.LatLng(lat,lng);
    	}else{
    		//默认地图中心为巴黎
    		var myLatlng=new google.maps.LatLng(48.8587741, 2.2074729);
    	}
        
        var mapOptions = {
            center: myLatlng,
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl:false,
    	    zoomControl:false,
    	    streetViewControl:false
    	};
        //googlemap(address,myLatlng,infowidowcontent,mapcontainer,mapOptions)
        //address='', myLatlng!='' 根据latlng确定位置
        //address!='', myLatlng='' 根据address查询地址
        googlemap('',myLatlng,'',$$('.playmethodaddrimg')[0],mapOptions);
    }
    //行程介绍地图
    if($$('#playingmethoddetail .page-content').data('type')=="TRACK"){
    	$$("#playingmethoddetail .todolistaddrimg").click(function(){
    		var playitem_id = $$(this).data('id');
    		$$.getJSON(url_get_playingmethoddetail_map, {id:playitem_id}, function (ajaxresult) {             
    			if(ajaxresult.code==1){
                    console.log(ajaxresult.data);
                    mainView.router.load({
                        "url":"playingmethoddetail_map.html",
                        "context":ajaxresult.data
                    });                     
                }else{
                    ajaxfail(ajaxresult.msg);
                }
            });
    	});
    }
    
    //导航点击
    $$("#playingmethoddetail .buttons-row .button").click(function(){
    	$$("#playingmethoddetail .buttons-row .button").removeClass('active');
    	$$(this).addClass("active");
    	var targetId="#"+$$(this).data('id');
    	var targetY=$$(targetId).offset().top+$$("#playingmethoddetail .page-content").scrollTop()-88;
    	$$("#playingmethoddetail .page-content").scrollTop(targetY);
    });
    $$("#playmethoddetail-subnav .buttons-row .button").click(function(){
    	$$("#playmethoddetail-subnav .buttons-row .button").removeClass('active');
    	$$(this).addClass("active");
    	var targetId="#"+$$(this).data('id');
    	var targetY=$$(targetId).offset().top+$$("#playingmethoddetail .page-content").scrollTop()-88;
    	$$("#playingmethoddetail .page-content").scrollTop(targetY);
    });
  
    var buttonrowY=$$("#playingmethoddetail .buttons-row").offset().top-44;
    //navbar和subnavbar高88px+2px
    var descY=$$("#desc").offset().top-90;
    var priceY=$$("#price").offset().top-90;
    var remarkY=$$("#remark").offset().top-90;
    //导航滚动
    $$("#playingmethoddetail .page-content").scroll(function(){
    	var top=$$(this).scrollTop();
    	//固定在顶部
    	if(top>buttonrowY){
    		if(!$$("#playingmethoddetail .buttons-row").hasClass('hidden')){
    			$$("#playingmethoddetail .buttons-row").addClass('hidden');
    		}    		
    		if($$("#playmethoddetail-subnav").hasClass('hidden')){
    			$$("#playmethoddetail-subnav").removeClass('hidden');
    		}
    	}else{
    		if($$("#playingmethoddetail .buttons-row").hasClass('hidden')){
    			$$("#playingmethoddetail .buttons-row").removeClass('hidden');
    		}    		
    		if(!$$("#playmethoddetail-subnav").hasClass('hidden')){
    			$$("#playmethoddetail-subnav").addClass('hidden');
    		}
    	}
    	//button active
    	if(top<descY){
    		$$("#playingmethoddetail .buttons-row .button").removeClass('active');
        	$$("#playingmethoddetail .buttons-row a[data-id='intro']").addClass("active");   		
        	$$("#playmethoddetail-subnav .buttons-row .button").removeClass('active');
        	$$("#playmethoddetail-subnav .buttons-row a[data-id='intro']").addClass("active");
    	}
    	else if(top>descY && top<priceY){
    		$$("#playingmethoddetail .buttons-row .button").removeClass('active');
        	$$("#playingmethoddetail .buttons-row a[data-id='desc']").addClass("active");   		
        	$$("#playmethoddetail-subnav .buttons-row .button").removeClass('active');
        	$$("#playmethoddetail-subnav .buttons-row a[data-id='desc']").addClass("active");
    	}
    	else if(top>priceY && top<remarkY){
    		$$("#playingmethoddetail .buttons-row .button").removeClass('active');
        	$$("#playingmethoddetail .buttons-row a[data-id='price']").addClass("active");   		
        	$$("#playmethoddetail-subnav .buttons-row .button").removeClass('active');
        	$$("#playmethoddetail-subnav .buttons-row a[data-id='price']").addClass("active");
    	}
    	else if(top>remarkY){
    		$$("#playingmethoddetail .buttons-row .button").removeClass('active');
        	$$("#playingmethoddetail .buttons-row a[data-id='remark']").addClass("active");   		
        	$$("#playmethoddetail-subnav .buttons-row .button").removeClass('active');
        	$$("#playmethoddetail-subnav .buttons-row a[data-id='remark']").addClass("active");
    	}
    });
    

    $$('#playingmethoddetail .msg').on('click',function(){
        var rev_id = $$(this).data('id');
        $$.getJSON(url_get_chat, {
            rev_id:rev_id,// 接收对象id
            page:1,
        }, function (ajaxresult) {
            if(ajaxresult.code==1){
                console.log(ajaxresult.data);
                mainView.router.load({
                    "url":"msg.html",
                    "context":ajaxresult.data
                }); 
            }else{
                ajaxfail(ajaxresult.msg);
            }
        });
    });
    $$('#playingmethoddetail .order').on('click',function(){
        var id = $$(this).data('id');
        var type = $$(this).data('type');
        $$.getJSON(url_get_getbookinginfo, {
            goodsId:id,// 接收对象id
            type:type,
        }, function (ajaxresult) {
            if(ajaxresult.code==1){
                console.log(ajaxresult.data.touristAreaCode);
                if(ajaxresult.data.content){
                	mainView.router.load({
                        "url":"order.html",
                        "context":ajaxresult.data,
                    });
                }else{ajaxfail("抱歉，该产品暂时没有可选的出行日期");} 
            }else{
                ajaxfail(ajaxresult.msg);
            }
        });
    });
    $$('#playingmethoddetail .remark').on('click',function(){
        var id = $$(this).data('id');
        mainView.router.load({
            "url":"remark.html",
            "context":{"id":id}
        }); 
    });
});
